package com.example.jetpacktest.viewmodel

import androidx.lifecycle.ViewModel
import java.util.*

class TimerViewModel : ViewModel() {
    var timer: Timer? = null
    var count = 0
    var countChangeListener: CountChangeListener? = null

    fun startCount() {
        if (timer == null) {
            timer = Timer()
            timer?.schedule(object : TimerTask() {
                override fun run() {
                    count++
                    countChangeListener?.onCountChanged(count)
                }

            }, 1000, 1000)
        }
    }


    override fun onCleared() {
        super.onCleared()
        timer?.cancel()
    }

    fun setTheCountChangeListener(countChangeListener: CountChangeListener?) {
        this.countChangeListener = countChangeListener
    }

    interface CountChangeListener {
        fun onCountChanged(count: Int)
    }
}